
app.directive("confirmdeleteuser", ['$rootScope', 'userServices', function($rootScope, userServices) {
    return {
        restrict: 'EA',
		templateUrl: "app/shared/directives/users/confirmdeleteuser.view.html",
        link: function(scope, element, attrs){
		
			/**
			* Confirm to delte user
			*/
			scope.deleteUserCommand = function(user) {
				try {		
					scope.loadingUsers = true;
					userServices.deleteUser (function (data) {
						console.debug('userServices deleteUser received');
						if(data.isFailed) {
							toastr.warning(data.exception, "Gestion des utilisateurs");
						}
						else {
							scope.users = data.response;
							scope.loadingUsers = false;
							$('#confirmDeleteUser').modal('hide');
							toastr.success("L'utilisateur a bien été supprimé.", "Gestion des utilisateurs");
						}
						
					}, user);
				}
				catch(exception) {
					toastr.error(exception, "Authentification");
				}
			};
			
			/**
			* Cancel delete user process
			*/
			scope.cancelDeleteUserCommand = function() {
				$('#confirmDeleteUser').modal('hide');
			};
        }
    };
}]);